<!-- 新增和编辑弹窗 -->
<template>
  <a-modal
    :width='800'
    :visible='visible'
    :confirm-loading='loading'
    :title="isUpdate ? '修改' : '新建'"
    :body-style="{ paddingBottom: '8px' }"
    @update:visible='updateVisible'
    @ok='save'
  >
    <a-form
      layout='horizontal'
      ref='form'
      :model='form'
      :rules='rules'
      :label-col='{ md: { span: 4 }, sm: { span: 24 } }'
      :wrapper-col='{ md: { span: 20 }, sm: { span: 24 } }'
    >

      <a-form-item label='设备编码:' name='recordNo'>
        <a-input v-model:value='form.recordNo' placeholder='请输入设备编码,类似终端出厂唯一序列号' allow-clear autocomplete='off' />
      </a-form-item>

      <a-form-item label="设备状态:" name="recordStatus">
        <a-select
          show-search
          v-model:value="form.recordStatus"
          placeholder="请输入设备状态"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
          allow-clear
        >
          <a-select-option value="1">正常</a-select-option>
          <a-select-option value="2">异常</a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="设备类型:" name="dceId">
        <a-select
          show-search
          v-model:value="form.dceId"
          placeholder="请选择设备类型"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
        >
          <a-select-option v-for="item in devicelist" :key="item.deviceId" >
            {{ item.deviceName }}
          </a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="所属牧场:" name="orgId">
        <a-select
          show-search
          v-model:value="form.orgId"
          placeholder="请选择所属牧场"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
          @change="selectRanch"
        >
          <a-select-option v-for="item in ranchList" :key="item.ranchId" >
            {{ item.ranchName }}
          </a-select-option>
        </a-select>
      </a-form-item>


      <a-form-item label="所属牛舍:" name="oxtId">
        <a-select
          show-search
          v-model:value="form.oxtId"
          placeholder="请选择所属牛舍"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
          @change="selectOxtall"
        >
          <a-select-option v-for="item in oxtallList" :key="item.oxtallId" >
            {{ item.oxtallName }}
          </a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="所属区域:" name="areaId">
        <a-select
          show-search
          v-model:value="form.areaId"
          placeholder="请选择所属区域"
          style="width: 100%"
          :show-arrow="false"
          :filter-option="false"
          :not-found-content="null"
        >
          <a-select-option v-for="item in areaList" :key="item.areaId" >
            {{ item.areaName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='固件版本号:' name='clientVersion'>
        <a-input v-model:value='form.clientVersion' placeholder='请输入固件版本号' allow-clear autocomplete='off' />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { DeviceRecordApi } from '@/api/property/DeviceRecordApi';
import { DeviceApi } from '@/api/property/DeviceApi';
import { RanchApi } from '@/api/property/RanchApi';
import { AreaApi } from '@/api/property/AreaApi';
import { OxtallApi } from '@/api/property/OxtallApi';
import { message } from 'ant-design-vue';

export default {
  name: 'DeviceRecordEdit',
  components: {
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  created() {
    if (this.data) {
      this.form = Object.assign({}, this.data);
      this.form.recordStatus = "" + this.form.recordStatus;
      if (this.form.oxtId <= 0) {
        this.form.oxtId = undefined;
      }
      if (this.form.orgId <= 0) {
        this.form.orgId = undefined;
      }
      if (this.form.areaId <= 0) {
        this.form.areaId = undefined;
      }
      this.isUpdate = true;
    } else {
      this.form = {};
      this.isUpdate = false;
    }
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        recordNo: [{ required: true, message: '请输入设备编码', type: 'string', trigger: 'blur' }],
        recordStatus: [{ required: true, message: '请输入设备状态', trigger: 'blur' }],
        dceId: [{ required: true, message: '请输入设备类型', trigger: 'blur' }],
        orgId: [{ required: true, message: '请输入所属牧场', trigger: 'blur' }],
        oxtId: [{ required: true, message: '请输入所属牛舍', trigger: 'blur' }],
        areaId: [{ required: true, message: '请输入所属区域', trigger: 'blur' }]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      //设备类型
      devicelist : [],
      ranchList : [],
      oxtallList : [],
      areaList : []
    };
  },
  async mounted() {
    // 查询菜单节点
    let deviceList = await DeviceApi.list({})
    this.devicelist = (deviceList ? deviceList.data : null) || [];
    let _ranchList  = await RanchApi.list({})
    this.ranchList = (_ranchList ? _ranchList.data : null) || [];
    if (this.form.orgId) {
      OxtallApi.list({"orgId" : this.form.orgId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    }
    if (this.form.oxtId) {
      AreaApi.list({"oxtId" : this.form.oxtId})
        .then(result => {
          this.areaList = (result ? result.data : null) || [];
        })
        .catch((err) => {
          console.log(err)
        });
    }
  },
  methods: {
    /**
     * 保存和编辑
     *
     * @author cancan
     * @date 2022/03/05 14:43
     */
    async save() {
      console.log(this.form);
      // 校验表单
      await this.$refs.form.validate();

      // 修改加载框为正在加载
      this.loading = true;

      let result = null;

      // 执行编辑或修改
      if (this.isUpdate) {
        result = DeviceRecordApi.edit(this.form);
      } else {
        result = DeviceRecordApi.add(this.form);
      }
      result
        .then(result => {
          // 移除加载框
          this.loading = false;

          // 提示添加成功
          message.success(result.message);

          // 如果是新增，则form表单置空
          if (!this.isUpdate) {
            this.form = {};
          }

          // 关闭弹框，通过控制visible的值，传递给父组件
          this.updateVisible(false);

          // 触发父组件done事件
          this.$emit('done');
        })
        .catch(() => {
          this.loading = false;
        });
    },

    /**
     * 更新编辑界面的弹框是否显示
     *
     * @param value true-显示，false-隐藏
     * @author cancan
     * @date 2022/03/05 14:43
     */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    selectRanch(ranchId) {
      this.oxtallList = [];
      this.areaList = []
      this.form.oxtId = undefined;
      this.form.areaId = undefined;
      OxtallApi.list({"orgId" : ranchId}).then(result => {
        this.oxtallList = (result ? result.data : null) || [];
      }).catch((err) => {
        console.log(err)
      });
    },
    selectOxtall(oxtallId) {
      this.areaList = []
      this.form.areaId = undefined;
      AreaApi.list({"oxtId" : oxtallId})
       .then(result => {
          this.areaList = (result ? result.data : null) || [];
        })
        .catch((err) => {
        console.log(err)
      });
    },

  }
};
</script>
